package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;



    /**
     * 角色分页
     *
     * @param roleDto
     * @return
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<RoleVo>> findByPage(
            @RequestBody RoleDto roleDto,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){
        PageResponse<RoleVo> roleVo = roleService.findByPage(roleDto, pageNum, pageSize);
        return ResponseResult.success(roleVo);
    }

    /**
     * 根据角色查询选中的资源数据
     *
     * @param roleId
     * @return
     */
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<Set<String>> QueryTheSelectedResourceDataBasedOnTheRole(@PathVariable("roleId") String roleId) {
        Set<String> resourceNos =roleService.QueryTheSelectedResourceDataBasedOnTheRole(roleId);
        return ResponseResult.success(resourceNos);
    }


    /**
     * 角色添加
     * @param roleDto
     * @return
     */
    @PutMapping
    public ResponseResult addRole(@RequestBody RoleDto roleDto) {
        Boolean flag =roleService.addRole(roleDto);
        return ResponseResult.success(flag);
    }


    /**
     * 角色修改
     * @param roleDto
     * @return
     */
    @PatchMapping
    public ResponseResult updateRole(@RequestBody RoleDto roleDto) {
        Boolean flag =roleService.updateRole(roleDto);
        return ResponseResult.success(flag);
    }


    /**
     * 角色删除
     * @param roleIds
     * @return
     */
    @DeleteMapping("/{roleIds}")
    public ResponseResult deleteRole(@PathVariable("roleIds") String roleIds) {
        roleService.deleteRole(roleIds);
        return ResponseResult.success();
    }


    @PostMapping("init-roles")
    @ApiOperation(value = "角色下拉框",notes = "角色下拉框")
    ResponseResult<List<RoleVo>> initRoles() {
        List<RoleVo> roleVoResult = roleService.initRoles();
        return ResponseResult.success(roleVoResult);
    }

}
